﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class TestsList : System.Web.UI.Page
{
    //static object userId;
    DataSet dsTest = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["State"] = "Scheduled";
            dsTest = AdminCreateTestDAL.GetTestListBasedOnStatus(Session["UserId"], "Scheduled");
            dsTest.Tables[0].Columns.Add("TopicName", typeof(System.String));
            dsTest.Tables[0].Columns.Add("TestAttemptId", typeof(System.Int32));
            dsTest.Tables[0].Columns.Add("AttemptNo", typeof(System.Int32));
            bindTopics();
            for (int i = 0; i < dsTest.Tables[0].Rows.Count; i++)
            {
                dsTest.Tables[0].Rows[0][7] = "0";
                dsTest.Tables[0].Rows[0][8] = "0";
            }
            grdTestList.DataSource = dsTest.Tables[0];
            grdTestList.DataBind();
            grdTestList.Columns[7].Visible = false;
            grdTestList.Columns[8].Visible = false;
            grdTestList.Columns[9].Visible = false;
        }
     }

    private void bindTopics()
    {
        int i, j;
        string topics;
        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            for (j = 0; j < dsTest.Tables[1].Rows.Count; j++)
            {
                if (dsTest.Tables[0].Rows[i][0].ToString() == dsTest.Tables[1].Rows[j][0].ToString())
                    dsTest.Tables[0].Rows[i]["TopicName"] += dsTest.Tables[1].Rows[j][1] + ",";
            }
        }
        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            topics = dsTest.Tables[0].Rows[i]["TopicName"].ToString();
            if (topics.Contains('|'))
            {
                topics = topics.Substring(0, topics.Length - 1);
            }
            dsTest.Tables[0].Rows[i]["TopicName"] = topics;
        }
    }


    protected void lnkTestScheduled_Click(object sender, EventArgs e)
    {
        Session["State"] = "Scheduled";
        grdTestList.Columns[7].Visible = false;
        grdTestList.Columns[8].Visible = false;
        grdTestList.Columns[9].Visible = false;
        grdTestList.Columns[10].Visible = true;
        grdTestList.SelectedIndex = -1;
        dsTest = AdminCreateTestDAL.GetTestListBasedOnStatus(Session["UserId"], "Scheduled");
        dsTest.Tables[0].Columns.Add("TopicName", typeof(System.String));
        dsTest.Tables[0].Columns.Add("TestAttemptId", typeof(System.Int32));
        dsTest.Tables[0].Columns.Add("AttemptNo", typeof(System.Int32));
        bindTopics();
        for (int i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            dsTest.Tables[0].Rows[0][7] = "0";
            dsTest.Tables[0].Rows[0][8] = "0";
        }
        grdTestList.DataSource = dsTest.Tables[0];
        grdTestList.DataBind();
    }
    protected void lnkTestPending_Click(object sender, EventArgs e)
    {
        Session["State"] = "Pending";
        grdTestList.Columns[7].Visible = false;
        grdTestList.Columns[8].Visible = false;
        grdTestList.Columns[9].Visible = false;
        grdTestList.Columns[10].Visible = false;
        grdTestList.SelectedIndex = -1;
        dsTest = AdminCreateTestDAL.GetTestListBasedOnStatus(Session["UserId"], "Pending");
        dsTest.Tables[0].Columns.Add("TopicName", typeof(System.String));
        dsTest.Tables[0].Columns.Add("AttemptNo", typeof(System.Int32));
        dsTest.Tables[0].Columns.Add("TestAttemptId", typeof(System.Int32));
        bindTopics();
        for (int i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            dsTest.Tables[0].Rows[0][7] = "0";
            dsTest.Tables[0].Rows[0][8] = "0";
        }
        grdTestList.DataSource = dsTest.Tables[0];
        grdTestList.DataBind();
    }
    protected void lnkTestCompteted_Click(object sender, EventArgs e)
    {
        Session["State"] = "Completed";
        grdTestList.Columns[3].Visible = false;
        grdTestList.Columns[7].Visible = true;
        grdTestList.Columns[8].Visible = true;
        grdTestList.Columns[9].Visible = true;
        grdTestList.Columns[10].Visible = true;
        grdTestList.SelectedIndex = -1;
        dsTest = AdminCreateTestDAL.GetTestCompletedListBasedOnStatus(Session["UserId"], "Completed");
        grdTestList.DataSource = dsTest.Tables[0];
        grdTestList.DataBind();
    }

    protected void grdTestList_SelectedIndexChanged(object sender, EventArgs e)
    {
        int testId = Convert.ToInt32(grdTestList.SelectedDataKey[0]);
        string status = grdTestList.SelectedDataKey[1].ToString();
        if(status.Trim()=="Scheduled")
        Response.Redirect("~/Student/TestInfo.aspx?TestId=" + testId);
    }

    protected void grdTestList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int testId = Convert.ToInt32(e.CommandArgument);
        

        switch (e.CommandName)
        {
            case "btnScoreClick": Response.Redirect("~/Student/Score.aspx?AttemptId=" + testId);
            break;

            case "btnLinkRankClick":
                if (Session["State"].ToString().Trim() == "Completed".Trim())
                {
                    DataTable dtTestId = new DataTable();
                    dtTestId = AdminCreateTestDAL.GetTestIdBasedOnAttemptId(testId);
                    testId = Convert.ToInt32(dtTestId.Rows[0][0]);
                }
                Response.Redirect("~/Student/ViewTestRank.aspx?TestId=" + testId.ToString());
            break;
        }  

        

    }
    
}